/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.google.common.collect.Lists;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.ArrayHandleUtils;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.dependencies.serv.utils.MybatisUtils;
import com.jnzc.ocr.serv.shop.consts.ShopDictConsts;
import com.jnzc.ocr.serv.shop.consts.ShopMarketingTypeConsts;
import com.jnzc.ocr.serv.shop.entity.ShopMarketingRule;
import com.jnzc.ocr.serv.shop.entity.ShopMarketingRule;
import com.jnzc.ocr.serv.shop.filter.ShopMarketingRuleFilter;
import com.jnzc.ocr.serv.shop.filter.ShopMarketingRuleFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopMarketingRuleMapper;
import com.jnzc.ocr.serv.shop.service.ShopMarketingRuleService;
import com.jnzc.ocr.serv.system.api.SysDictApi;
import com.jnzc.ocr.serv.system.vo.DictPair;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Service
public class ShopMarketingRuleServiceImpl extends MybatisServiceImpl<ShopMarketingRuleMapper, ShopMarketingRule, ShopMarketingRuleFilter> implements ShopMarketingRuleService {

    @Autowired
    private ShopMarketingRuleMapper shopMarketingRuleMapper;

    @Autowired
    private SysDictApi sysDictApi;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopMarketingRule> ew, ShopMarketingRuleFilter filter) {
        ShopMarketingRule entity = new ShopMarketingRule();
        entity.setId(filter.getId());
        entity.setShopId(filter.getShopId());
        entity.setMarketingType(filter.getMarketingType());
        ew.setEntity(entity);
        if(CollectionUtils.isNotEmpty(filter.getMarketingTypeList())) {
            ew.in("marketing_type", filter.getMarketingTypeList());
        }
    }

    @Override
    public ShopMarketingRule find(Long id, Long shopId) {
        ValidateUtils.isId(id, "营销规则ID");
        ShopMarketingRuleFilter filter = new ShopMarketingRuleFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        return super.findNotNull(filter);
    }

    @Override
    public List<ShopMarketingRule> list(List<Integer> marketingTypeList, Long shopId) {
        if(CollectionUtils.isEmpty(marketingTypeList)) {
            return Lists.newArrayList();
        }
        ShopMarketingRuleFilter filter = new ShopMarketingRuleFilter();
        filter.setMarketingTypeList(marketingTypeList);
        filter.setShopId(shopId);
        filter.setOrderBy("amount_line");
        return super.list(filter);
    }

    @Override
    public Map<Integer, List<ShopMarketingRule>> listMap(List<Integer> marketingTypeList, Long shopId) {
        List<ShopMarketingRule> list = this.list(marketingTypeList, shopId);
        return ArrayHandleUtils.parseMapGroup(list, "marketingType");
    }

    @Override
    public void delete(Long id) {
        shopMarketingRuleMapper.deleteById(id);
    }

    @Override
    public ShopMarketingRule save(ShopMarketingRule obj) {
        ValidateUtils.isId(obj.getShopId(), "店铺ID");
        ValidateUtils.notNull(obj.getMarketingType(), "营销类型");
        if(null == obj.getMarketingTypeName()){
            DictPair dictPair = sysDictApi.findPair(ShopDictConsts.SHOP_MARKETING_TYPE, obj.getMarketingType());
            obj.setMarketingTypeName(dictPair.getName());
        }
        if(ShopMarketingTypeConsts.POINT == obj.getMarketingType()){
            ValidateUtils.notNull(obj.getPoint(), "积分");
            obj.setDescription(MessageFormat.format("冲{0,number,#.##}送{1,number,#}", obj.getAmountLine(), obj.getPoint()));
        } else if(ShopMarketingTypeConsts.AMOUNT == obj.getMarketingType()){
            ValidateUtils.notNull(obj.getAmount(), "金额");
            obj.setDescription(MessageFormat.format("满{0,number,#.##}送{1,number,#.##}元", obj.getAmountLine(), obj.getAmount()));
        } else if(ShopMarketingTypeConsts.DISCOUNT == obj.getMarketingType()){
            ValidateUtils.notNull(obj.getDiscount(), "折扣");
            obj.setDescription(MessageFormat.format("满{0,number,#.##}打{1,number,#.##}折", obj.getAmountLine(), obj.getDiscount()));
        } else {
            obj.setDescription(null);
        }
        if(null == obj.getId()){
            obj.setCreateTime(DateUtils.getNow());
            shopMarketingRuleMapper.insert(obj);
        } else {
            shopMarketingRuleMapper.updateById(obj);
        }
        return obj;
    }
}
